package com.smartleanx.module.hrm.service.payrollapproverecord;

import com.smartleanx.framework.common.enums.BizTypeEnum;
import com.smartleanx.framework.common.pojo.PageResult;
import com.smartleanx.framework.common.util.collection.CollectionUtils;
import com.smartleanx.framework.common.util.object.BeanUtils;
import com.smartleanx.framework.customfield.core.service.CustomFieldDataService;
import com.smartleanx.framework.customfield.core.service.bo.CustomFieldDataSaveBO;
import com.smartleanx.framework.customfield.core.service.bo.inner.CustomFieldDataBaseBO;
import com.smartleanx.module.hrm.controller.admin.payrollapproverecord.vo.PayrollApproveRecordDetailPageReqVO;
import com.smartleanx.module.hrm.dal.dataobject.payrollapproverecord.PayrollApproveRecordDetailDO;
import com.smartleanx.module.hrm.dal.mysql.payrollapproverecord.PayrollApproveRecordDetailMapper;
import com.smartleanx.module.hrm.service.payrollapproverecord.bo.PayrollApproveRecordDetailSaveBO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.smartleanx.module.hrm.enums.ErrorCodeConstants.PAYROLL_APPROVE_RECORD_DETAIL_NOT_EXISTS;

/**
 * 薪资审批记录详情 Service 实现类
 *
 * @author zhangchp
 */
@Service
@Validated
public class PayrollApproveRecordDetailServiceImpl implements PayrollApproveRecordDetailService {

    @Resource
    private PayrollApproveRecordDetailMapper payrollApproveRecordDetailMapper;

    @Resource
    private CustomFieldDataService customFieldDataService;

    @Override
    @Transactional(rollbackFor = Exception.class) // 添加事务，异常则回滚所有导入
    public void createDetail(Collection<PayrollApproveRecordDetailSaveBO> saveBOs, Collection<CustomFieldDataBaseBO> fieldDataList) {
        // 插入
        List<PayrollApproveRecordDetailDO> detailList = BeanUtils.toBean(saveBOs, PayrollApproveRecordDetailDO.class);
        payrollApproveRecordDetailMapper.insertBatch(detailList);
        Map<Long, Long> idMap = CollectionUtils.convertMap(detailList, PayrollApproveRecordDetailDO::getPayrollDetailId, PayrollApproveRecordDetailDO::getId);
        fieldDataList.forEach(data -> data.setRecordId(idMap.get(data.getRecordId())));
        customFieldDataService.crudData(CustomFieldDataSaveBO.builder().datas(fieldDataList).bizType(BizTypeEnum.PAYROLL.getValue())
                .edition(BizTypeEnum.PAYROLL_APPROVAL.getValue()).labels(CollectionUtils.convertSet(fieldDataList, CustomFieldDataBaseBO::getLabel))
                .labelGroups(CollectionUtils.convertSet(fieldDataList, CustomFieldDataBaseBO::getLabelGroup))
                .recordIds(CollectionUtils.convertSet(fieldDataList, CustomFieldDataBaseBO::getRecordId)).build());
    }


    private void validatePayrollApproveRecordDetailExists(Long id) {
        if (payrollApproveRecordDetailMapper.selectById(id) == null) {
            throw exception(PAYROLL_APPROVE_RECORD_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public List<PayrollApproveRecordDetailDO> getDetailByPayrollApproveRecordId(Long payrollApproveRecordId) {
        return payrollApproveRecordDetailMapper.selectByPayrollApproveRecordId(payrollApproveRecordId);
    }

    @Override
    public PageResult<PayrollApproveRecordDetailDO> getDetailPage(PayrollApproveRecordDetailPageReqVO pageReqVO) {
        return payrollApproveRecordDetailMapper.selectPage(pageReqVO);
    }

}